package com.xaicode.template.activemq.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class JMS_consumer_topic_test {

    public static final String ACTIVEMQ_URL = "tcp://192.168.18.182:61616";
    public static final String TOPIC_NAME = "CRZS_TRANSFER_SYSTEM";


    /**
     * 1、一次运行订阅此公众号 注册一次 然后关闭程序
     * 2、再次运行依然可以收到持久化消息
     *
     * @param args
     * @throws JMSException
     */
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.setClientID("z3");
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Topic topic = session.createTopic(TOPIC_NAME);

        MessageConsumer messageConsumer = session.createConsumer(topic);

        messageConsumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                if (message instanceof MapMessage) {
                    MapMessage mapMessage = (MapMessage) message;
                    try {
                        System.out.println(mapMessage.getString("Msg"));
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        messageConsumer.close();
        session.close();
        connection.close();
    }
}
